﻿using HUNTERS.CORE.Models.Request;
using HUNTERS.CORE.Models.Response;
using HUNTERS.HELPERS;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;

namespace HUNTERS.CORE
{
    public class Services
    {
        #region 声明
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private CookieCollection _cookies;

        private LoginInfo _loginInfo;

        public GetCurrentQuestions currentQuestions;

        private string scnt, sstt, sessionId, referer, apiKey;

        private string proxyIp = "";

        public static string widgetKey;

        public static Action<Services, MessageEventArgs> OnEvent;

        private static object lock_Object = new object();

        #endregion

        public Services()
        {
            _cookies = new CookieCollection();
            lock (lock_Object)
            {
                if (string.IsNullOrEmpty(widgetKey))
                {
                    GetServiceKey();
                }
            }
        }

        #region AppleId API
        /// <summary>
        /// 获取 SERVICEKEY
        /// </summary>
        /// <returns></returns>
        public bool GetServiceKey()
        {
            Send("正在取得SERVICEKEY信息...");
            WebHeaderCollection header = new WebHeaderCollection()
            {
                {"X-Apple-Upgrade-Insecure-Requests", "1"},
                {"Accept-Encoding",  "gzip, deflate, br"},
                {"Accept-Language","zh-CN,zh;q=0.8"}
            };

            HttpItem request = new HttpItem()
            {
                URL = "https://appleid.apple.com",
                KeepAlive = true,
                Header = header,
                ProxyIp = proxyIp
            };

            HttpResult result = HTTPHelpers.GetResponse(request, _cookies);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                Send("SERVICEKEY 请求访问失败!");
                return false;
            }

            var serviceKey = Regex.Match(result.Html, "serviceKey: '(?<key>.*?)'");

            if (!serviceKey.Success)
            {
                Send("获取 SERVICEKEY 失败");
                return false;
            }

            widgetKey = serviceKey.Groups["key"].Value;
            Send("成功获取SERVICEKEY信息!");

            return true;
        }

        /// <summary>
        /// 登录初始化
        /// </summary>
        /// <param name="loginInfo"></param>
        /// <returns></returns>
        public bool CheckProxy(string proxy)
        {
            proxyIp = proxy;

            HttpItem request = new HttpItem()
            {
                URL = "https://www.apple.com/ac/globalnav/3/zh_CN/images/globalnav/search/image_large.svg",
                ProxyIp = proxyIp,
                Timeout = 3000,
            };

            HttpResult result = HTTPHelpers.GetResponse(request, _cookies);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                Send(string.Format("代理 {0} 无效", proxy));
                return false;
            }

            Send(string.Format("代理 {0} 正常", proxy));
            return true;
        }

        /// <summary>
        /// 登录初始化
        /// </summary>
        /// <param name="loginInfo"></param>
        /// <returns></returns>
        public bool LoginInit(LoginInfo loginInfo)
        {
            _loginInfo = loginInfo;
            return true;
        }

        /// <summary>
        /// 账号登录
        /// </summary>
        /// <returns></returns>
        /// https://idmsa.apple.com/appleauth/auth/signin
        public bool Login()
        {
            Send("开始执行登录...");
            WebHeaderCollection header = new WebHeaderCollection()
            {
                {"X-Apple-Domain-Id", "1"},
                {"X-Apple-Widget-Key", widgetKey},
                {"X-Apple-Locale", "zh_CN"},
                {"X-Requested-With", "XMLHttpRequest"},
                {"Accept-Language","zh-CN,zh;q=0.8"},
                {"X-Apple-I-FD-Client-Info",""  }//""
            };

            HttpItem request = new HttpItem()
            {
                URL = "https://idmsa.apple.com/appleauth/auth/signin",
                Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                Postdata = JsonConvert.SerializeObject(_loginInfo),
                Header = header,
                Accept = "application/json, text/javascript, */*; q=0.01",
                ContentType = "application/json",
                ProxyIp = proxyIp
            };

            HttpResult result = HTTPHelpers.PostResponse(request, _cookies);

            if (result.StatusCode == HttpStatusCode.ServiceUnavailable)
            {
                Send("此访问请求已被限制,请更换网络。");
                return false;
            }

            if (result.StatusCode == HttpStatusCode.Unauthorized)
            {
                Send("Apple Id 或密码不正确。");
                return false;
            }

            if (result.StatusCode == HttpStatusCode.Forbidden)
            {
                Send("此 Apple ID 已被锁定。");
                return false;
            }

            if (result.StatusCode == HttpStatusCode.PreconditionFailed)
            {
                Send("此 Apple ID 未设置密码保护。");
                return false;
            }

            if (result.StatusCode == HttpStatusCode.Conflict)
            {
                var resultObject = JsonConvert.DeserializeObject<LoginSuccess>(result.Html);
                if (!string.IsNullOrEmpty(resultObject.authType))
                {
                    if (resultObject.authType == "hsa2")
                    {
                        Send("此 Apple ID 需进行二次验证。");
                        return false;
                    }

                    if (resultObject.authType == "sa")
                    {
                        scnt = result.Header.Get("scnt");
                        sessionId = result.Header.Get("X-Apple-ID-Session-Id");
                        Send("登录成功!");
                        return true;
                    }
                }

            }
            Send("登录异常!");
            logger.Error(JsonConvert.SerializeObject(result));
            return false;
        }

        /// <summary>
        /// 获取密保信息
        /// </summary>
        /// <returns></returns>
        public bool GetQuestions()
        {
            Send("开始执行获取密保信息...");
            WebHeaderCollection header = new WebHeaderCollection()
            {
                {"X-Apple-Domain-Id", "1"},
                {"X-Apple-Widget-Key", widgetKey},
                {"X-Apple-Locale", "zh_CN"},
                {"X-Requested-With", "XMLHttpRequest"},
                {"Accept-Language","zh-CN,zh;q=0.8"},
                {"X-Apple-I-FD-Client-Info", "" },
                {"X-Apple-ID-Session-Id", sessionId },
                {"scnt", scnt}
            };


            HttpItem request = new HttpItem()
            {
                URL = "https://idmsa.apple.com/appleauth/auth",
                Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                Header = header,
                ProxyIp = proxyIp
            };

            // idmsa.apple.com/appleauth/auth
            HttpResult result = HTTPHelpers.GetResponse(request, _cookies);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                Send("GetQuestions 请求访问失败!");
                return false;
            }

            request = new HttpItem()
            {
                URL = "https://idmsa.apple.com/appleauth/auth/questions/reset/iforgot",
                Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                Accept = "application/json, text/javascript, */*; q=0.01",
                ContentType = "application/json",
                Header = header,
                ProxyIp = proxyIp
            };

            // idmsa.apple.com/appleauth/auth/questions/reset/iforgot
            result = HTTPHelpers.GetResponse(request, _cookies);

            Debug.WriteLine(result.StatusCode);

            if (result.StatusCode == HttpStatusCode.NoContent)
            {
                request = new HttpItem()
                {
                    // iforgot.apple.com/request/questions/reset?prs_account_nm=
                    URL = result.RedirectUrl + "returnUrl=https://appleid.apple.com/#!&page=signin",
                    Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                    ProxyIp = proxyIp
                };


                result = HTTPHelpers.GetResponse(request, _cookies);


                if (result.StatusCode == HttpStatusCode.Found)
                {
                    request = new HttpItem()
                    {
                        // iforgot.apple.com/questions/reset/options?sstt=NEZt4myZc9iIIgZK8kWubm3NoZF8GgpTlSDB4zUgBXwC9PZSMKMCnCCnLm53
                        URL = result.RedirectUrl,
                        Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                        ProxyIp = proxyIp
                    };

                    result = HTTPHelpers.GetResponse(request, _cookies);

                    //匹配两个字符串A与B中间的字符串且不包含A与B：
                    //表达式: (?<=A).*?(?=B) 示例: Awww.baidu.comB  遇见特殊符号用 \

                    //无需选择 验证方式
                    if (result.StatusCode == HttpStatusCode.Found)
                    {
                        Send("使用模式①");

                        if (!result.RedirectUrl.Contains("questions/verify/questions"))
                        {
                            Send("无法重设安全提示问题");
                            return false;
                        }

                        request = new HttpItem()
                        {
                            URL = result.RedirectUrl,
                            Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                            ProxyIp = proxyIp
                        };

                        result = HTTPHelpers.GetResponse(request, _cookies);

                        var data = Regex.Match(result.Html, "(?<=data: ).*?(?=,\\n)");

                        if (!data.Success)
                        {
                            Send("获取安全问题失败");
                            return false;
                        }

                        referer = result.RedirectUrl;
                        currentQuestions = JsonConvert.DeserializeObject<GetCurrentQuestions>(data.Value);
                        sstt = currentQuestions.sstt;
                        Send("获取安全问题成功");
                        return true;
                    }

                    //绑定手机 电子邮件 需要选择
                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        Send("使用模式②");
                        var errorCheck = Regex.Match(result.Html, "(?<=data: ).*?(?=,\\n)");

                        if (!errorCheck.Success)
                        {
                            Send("errorCheck 匹配失败");
                            return false;
                        }

                        if (errorCheck.Value.Contains("\"resetOptions\":[]"))
                        {
                            Send("无法重设安全提示问题");
                            return false;
                        }

                        sstt = Regex.Match(result.Html, "(?<=encodeURIComponent\\(\").*?(?=\")").Value;

                        header = new WebHeaderCollection()
                            {
                                {"Accept-Language","zh-CN"},
                                {"X-Requested-With","XMLHttpRequest" },
                                {"X-Apple-I-FD-Client-Info", "" },
                                {"sstt", HttpUtility.UrlEncode(sstt)},
                                {"X-Requested-With","XMLHttpRequest"}
                            };

                        request = new HttpItem()
                        {
                            URL = "https://iforgot.apple.com/questions/reset/options",
                            Postdata = "{\"questionsResetOption\":\"question\"}",
                            Referer = result.RedirectUrl,
                            Accept = "application/json, text/javascript, */*; q=0.01",
                            ContentType = "application/json",
                            KeepAlive = true,
                            Allowautoredirect = true,
                            Header = header,
                            ProxyIp = proxyIp
                        };

                        result = HTTPHelpers.PostResponse(request, _cookies);
                        referer = result.RedirectUrl;
                        currentQuestions = JsonConvert.DeserializeObject<GetCurrentQuestions>(result.Html);
                        sstt = currentQuestions.sstt;
                        Send("获取安全问题成功");
                        return true;
                    }
                }

            }
            logger.Error(JsonConvert.SerializeObject(result));
            return false;
        }



        public bool GuessQuestions(VerifyQuestionsItem verifyquestion)
        {
            Send("开始执行匹配密保信息...");
            WebHeaderCollection header = new WebHeaderCollection()
            {
                {"Accept-Language","zh-CN"},
                {"X-Apple-I-FD-Client-Info", "" },
                {"sstt", HttpUtility.UrlEncode(currentQuestions.sstt)},
                {"X-Requested-With","XMLHttpRequest"}
            };
            HttpItem request = new HttpItem()
            {
                URL = "https://iforgot.apple.com/questions/verify/questions",
                Accept = "application/json, text/javascript, */*; q=0.01",
                ContentType = "application/json",
                Header = header,
                Referer = referer,
                Postdata = JsonConvert.SerializeObject(verifyquestion),
                Allowautoredirect = true,
                ProxyIp = proxyIp
            };

            var result = HTTPHelpers.PostResponse(request, _cookies);

            if (result.StatusCode == HttpStatusCode.BadRequest)
            {
                Send(string.Format("尝试匹配问题   [{0}]  -  QuestID : {1}   失败", verifyquestion.number, verifyquestion.id));
            }

            if (result.StatusCode == HttpStatusCode.OK)
            {
                Send(string.Format("尝试匹配问题   [{0}]  -  QuestID : {1}   成功", verifyquestion.number, verifyquestion.id));
                var availableQuestions = JsonConvert.DeserializeObject<AvailableQuestions>(result.Html);
                sstt = availableQuestions.sstt;
                return true;
            }
            
            return false;
        }

        public bool GuessQuestions(List<GuessQuestions> guessQuestionsItems)
        {
            Send("开始执行匹配密保信息...");
            WebHeaderCollection header = new WebHeaderCollection()
            {
                {"Accept-Language","zh-CN"},
                {"X-Apple-I-FD-Client-Info", "" },
                {"sstt", HttpUtility.UrlEncode(currentQuestions.sstt)},
                {"X-Requested-With","XMLHttpRequest"}
            };

            var quests = currentQuestions.questions.Join(guessQuestionsItems, c => c.id, g => g.id, (c, g) => new GuessQuestions()
            {
                id = g.id,
                question = g.question,
                answer = g.answer,
                number = c.number,
                level = g.level
            }).OrderBy(m => m.level).ThenBy(m => m.id).ToList();

            foreach (var quest in quests)
            {
                List<VerifyQuestionsItem> verifyQuestionsItem = new List<VerifyQuestionsItem>();
                verifyQuestionsItem.Add(quest);

                VerifyQuestions verifyQuestions = new VerifyQuestions()
                {
                    questions = verifyQuestionsItem,
                };
                
                HttpItem request = new HttpItem()
                {
                    URL = "https://iforgot.apple.com/questions/verify/questions",
                    Accept = "application/json, text/javascript, */*; q=0.01",
                    ContentType = "application/json",
                    Header = header,
                    Referer = referer,
                    Postdata = JsonConvert.SerializeObject(verifyQuestions),
                    Allowautoredirect = true,
                    ProxyIp = proxyIp
                };

                var result = HTTPHelpers.PostResponse(request, _cookies);

                if (result.StatusCode == HttpStatusCode.BadRequest)
                {
                    Send(string.Format("尝试匹配问题   [{0}]  -  QuestID : {1}   失败", quest.number, quest.id));
                }

                if (result.StatusCode == HttpStatusCode.OK)
                {
                    Send(string.Format("尝试匹配问题   [{0}]  -  QuestID : {1}   成功", quest.number, quest.id));
                    var availableQuestions = JsonConvert.DeserializeObject<AvailableQuestions>(result.Html);
                    sstt = availableQuestions.sstt;
                    return true;
                }
            }
            return false;
        }

        public bool ResetQuestions(List<SelectedQuestionsItem> resetQuestionsItems)
        {
            Send("开始执行重设密保信息...");

            ResetQuestions resetQuestions = new ResetQuestions();
            resetQuestions.selectedQuestions = resetQuestionsItems;

            WebHeaderCollection header = new WebHeaderCollection()
            {
                {"Accept-Language","zh-CN,zh;q=0.9,en;q=0.8"},
                {"X-Apple-I-FD-Client-Info", "" },
                {"sstt", HttpUtility.UrlEncode(sstt)},
                {"X-Requested-With","XMLHttpRequest"}
            };

            HttpItem request = new HttpItem()
            {
                URL = "https://iforgot.apple.com/questions/reset",
                Accept = "application/json, text/javascript, */*; q=0.01",
                ContentType = "application/json",
                Postdata = JsonConvert.SerializeObject(resetQuestions),
                Header = header,
                Referer = referer,
                ProxyIp = proxyIp
            };

            var result = HTTPHelpers.PostResponse(request, _cookies);

            if ((int)result.StatusCode == 260)
            {
                Send(string.Format("重设密保信息成功"));
                return true;
            }

            Send(string.Format("重设密保信息失败"));
            logger.Error(JsonConvert.SerializeObject(result));
            return false;
        }

        public bool AccountManageLogin(List<SelectedQuestionsItem> resetQuestionsItems)
        {
            _cookies = new CookieCollection();

            if (!Login()) return false;

            Send("开始执行获取登录密保验证问题...");
            WebHeaderCollection header = new WebHeaderCollection()
            {
                {"X-Apple-Domain-Id", "1"},
                {"X-Apple-Widget-Key", widgetKey},
                {"X-Apple-Locale", "zh_CN"},
                {"X-Requested-With", "XMLHttpRequest"},
                {"Accept-Language","zh-CN,zh;q=0.8"},
                {"X-Apple-I-FD-Client-Info", "" },
                {"X-Apple-ID-Session-Id", sessionId },
                {"scnt", scnt}
            };


            HttpItem request = new HttpItem()
            {
                URL = "https://idmsa.apple.com/appleauth/auth",
                Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                Header = header,
                ProxyIp = proxyIp
            };

            HttpResult result = HTTPHelpers.GetResponse(request, _cookies);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                Send("AccountManageLogin 请求访问失败!");
                return false;
            }

            var data = Regex.Match(result.Html, "(?<=bootData.data.twoSV =).*?(?=;)").Value;
            var questions = JsonConvert.DeserializeObject<GetSecurityQuestions>(data).securityQuestions.questions;

            List<VerifyQuestionsItem> verifyQuestionsItems = new List<VerifyQuestionsItem>();

            foreach (var question in questions)
            {
                SelectedQuestionsItem resetQuestionsItem = resetQuestionsItems.FirstOrDefault(m => m.id == question.id.ToString());

                VerifyQuestionsItem verifyQuestionsItem = new VerifyQuestionsItem();
                verifyQuestionsItem.id = question.id;
                verifyQuestionsItem.question = question.question;
                verifyQuestionsItem.answer = resetQuestionsItem.answer;
                verifyQuestionsItem.number = question.number;

                verifyQuestionsItems.Add(verifyQuestionsItem);
            }

            VerifyQuestions verifyQuestions = new VerifyQuestions();
            verifyQuestions.questions = verifyQuestionsItems;

            request = new HttpItem()
            {
                URL = "https://idmsa.apple.com/appleauth/auth/verify/questions",
                Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                Accept = "application/json, text/javascript, */*; q=0.01",
                ContentType = "application/json",
                Header = header,
                Postdata = JsonConvert.SerializeObject(verifyQuestions),
                ProxyIp = proxyIp
            };

            result = HTTPHelpers.PostResponse(request, _cookies);

            if (result.StatusCode == HttpStatusCode.PreconditionFailed)
            {
                Send(string.Format("确认隐私协议..."));
                request = new HttpItem()
                {
                    URL = "https://appleid.apple.com/account/manage/privacy/accept",
                    Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                    Accept = "application/json, text/javascript, */*; q=0.01",
                    ContentType = "application/json",
                    Header = header,
                    ProxyIp = proxyIp
                };

                result = HTTPHelpers.PUTResponse(request, _cookies);

                if (result.StatusCode != HttpStatusCode.OK)
                {
                    request = new HttpItem()
                    {
                        URL = "https://idmsa.apple.com/appleauth/auth/repair/complete",
                        Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + widgetKey + "&language=zh_CN&rv=1",
                        Accept = "application/json, text/javascript, */*; q=0.01",
                        ContentType = "application/json",
                        Header = header,
                        ProxyIp = proxyIp
                    };

                    result = HTTPHelpers.PostResponse(request, _cookies);
                }
                else
                {
                    Send(string.Format("确认隐私协议失败."));
                }
            }

            if ((int)result.StatusCode == 204)
            {
                Send(string.Format("登录问题验证已通过!登录成功..."));
                request = new HttpItem()
                {
                    URL = "https://appleid.apple.com/account/manage",
                    Referer = "https://appleid.apple.com/",
                    ProxyIp = proxyIp
                };

                result = HTTPHelpers.GetResponse(request, _cookies);

                if (result.StatusCode == HttpStatusCode.OK)
                {
                    apiKey = Regex.Match(result.Html, "(?<=data.apiKey = ').*?(?=';)").Value;
                    Send(string.Format("跳转用户控制面板成功."));
                    return true;
                }
            }

            Send(string.Format("登录问题验证验证失败."));
            logger.Error(JsonConvert.SerializeObject(result));
            return false;
        }

        public string GetBirthday()
        {
            Send(string.Format("开始获取用户生日信息."));
            HttpItem request = new HttpItem()
            {
                URL = "https://appleid.apple.com/account/manage",
                Referer = "https://appleid.apple.com/",
                ProxyIp = proxyIp
            };

            var result = HTTPHelpers.GetResponse(request, _cookies);

            if (result.StatusCode == HttpStatusCode.OK)
            {
                var data = Regex.Match(result.Html, "(?<=data.account =\n).*?(?=\n;)").Value;
                var accountInfo = JsonConvert.DeserializeObject<GetAccountInfo>(data);
                Send("成功获取用户生日信息为 : " + accountInfo.security.birthday.ToString("yyyy-MM-dd"));
                return accountInfo.security.birthday.ToString("yyyy-MM-dd");
            }

            Send(string.Format("获取用户生日信息失败."));
            logger.Error(JsonConvert.SerializeObject(result));
            return string.Empty;
        }

        public string GetPayment()
        {
            Send(string.Format("开始获取用户支付信息."));
            WebHeaderCollection header = new WebHeaderCollection()
                {
                    {"X-Apple-Api-Key", apiKey},
                    {"X-Apple-I-FD-Client-Info", "" },
                    {"X-Requested-With", "XMLHttpRequest"},
                    {"Accept-Language","zh-CN,zh;q=0.9,en;q=0.8"},
                };

            HttpItem request = new HttpItem()
            {
                URL = "https://appleid.apple.com/account/manage/payment",
                Referer = "https://appleid.apple.com/account/manage",
                Accept = "application/json; charset=utf-8",
                ContentType = "application/json",
                Header = header,
                ProxyIp = proxyIp
            };

            var result = HTTPHelpers.GetResponse(request, _cookies);

            if (result.StatusCode == HttpStatusCode.OK)
            {
                var payment = JsonConvert.DeserializeObject<GetPayment>(result.Html);
                Send("成功获取用户支付信息为 : " + payment.primaryPaymentMethod.paymentMethodName);
                return payment.primaryPaymentMethod.paymentMethodName;
            }


            Send(string.Format("获取用户支付信息失败."));
            logger.Error(JsonConvert.SerializeObject(result));
            return string.Empty;
        }

        public bool ResetBirthday(DateTime birthDate)
        {
            WebHeaderCollection header = new WebHeaderCollection()
                    {
                        {"X-Apple-Api-Key", apiKey},
                        {"X-Requested-With", "XMLHttpRequest"},
                        {"X-Apple-I-FD-Client-Info", "" },
                        {"Accept-Language","zh-CN,zh;q=0.9,en;q=0.8"},
                    };

            HttpItem request = new HttpItem()
            {
                URL = "https://appleid.apple.com/account/manage/security/birthday",
                Referer = "https://appleid.apple.com/account/manage",
                Accept = "application/json",
                ContentType = "application/json",
                Header = header,
                Postdata = "{\"fullBirthDate\":\"" + birthDate.ToString("yyyy-MM-dd") + "\"}",
                ProxyIp = proxyIp
            };

            var result = HTTPHelpers.PUTResponse(request, _cookies);

            if (result.StatusCode == HttpStatusCode.OK)
            {
                Send(string.Format("修改生日信息成功."));
                return true;
            }


            Send(string.Format("修改生日信息失败."));
            logger.Error(JsonConvert.SerializeObject(result));
            return false;
        }




        #endregion

        #region 方法
        private void Send(string msg)
        {
            if (_loginInfo != null)
            {
                msg = "[" + _loginInfo.accountName + "] : " + msg;
            }
            OnEvent?.Invoke(this, new MessageEventArgs(msg));
        }

        public string getIP()
        {
            HttpItem request = new HttpItem()
            { 
                URL = "http://www.3322.org/dyndns/getip",
                ProxyIp = proxyIp
            };

            var result = HTTPHelpers.GetResponse(request, _cookies);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                return string.Empty;
            }
            return result.Html;
        }
        #endregion
    }
}
